{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lab-03-2 Minimizing Cost Gradinet Update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "tf.set_random_seed(777)  # for reproducibility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## X and Y data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x_data = [1, 2, 3]\n",
    "y_data = [1, 2, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Try to find values for W and b to compute y_data = W * x_data + b\n",
    "# We know that W should be 1 and b should be 0\n",
    "# But let's use TensorFlow to figure it out\n",
    "W = tf.Variable(tf.random_normal([1]), name='weight')\n",
    "\n",
    "X = tf.placeholder(tf.float32)\n",
    "Y = tf.placeholder(tf.float32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Our Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Our hypothesis for linear model X * W\n",
    "hypothesis = X * W\n",
    "\n",
    "# cost/loss function\n",
    "cost = tf.reduce_mean(tf.square(hypothesis - Y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Minimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Minimize: Gradient Descent using derivative: W -= learning_rate * derivative\n",
    "learning_rate = 0.1\n",
    "gradient = tf.reduce_mean((W * X - Y) * X)\n",
    "descent = W - learning_rate * gradient\n",
    "update = W.assign(descent)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Prepare session"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Launch the graph in a session.\n",
    "sess = tf.Session()\n",
    "# Initializes global variables in the graph.\n",
    "sess.run(tf.global_variables_initializer())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Fit the line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1.93919 [ 1.64462376]\n",
      "1 0.551591 [ 1.34379935]\n",
      "2 0.156897 [ 1.18335962]\n",
      "3 0.0446285 [ 1.09779179]\n",
      "4 0.0126943 [ 1.05215561]\n",
      "5 0.00361082 [ 1.0278163]\n",
      "6 0.00102708 [ 1.01483536]\n",
      "7 0.000292144 [ 1.00791216]\n",
      "8 8.30968e-05 [ 1.00421977]\n",
      "9 2.36361e-05 [ 1.00225055]\n",
      "10 6.72385e-06 [ 1.00120032]\n",
      "11 1.91239e-06 [ 1.00064015]\n",
      "12 5.43968e-07 [ 1.00034142]\n",
      "13 1.54591e-07 [ 1.00018203]\n",
      "14 4.39416e-08 [ 1.00009704]\n",
      "15 1.24913e-08 [ 1.00005174]\n",
      "16 3.5322e-09 [ 1.00002754]\n",
      "17 9.99824e-10 [ 1.00001466]\n",
      "18 2.88878e-10 [ 1.00000787]\n",
      "19 8.02487e-11 [ 1.00000417]\n",
      "20 2.34053e-11 [ 1.00000226]\n"
     ]
    }
   ],
   "source": [
    "for step in range(21):\n",
    "    sess.run(update, feed_dict={X: x_data, Y: y_data})\n",
    "    print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "0 5.81756 [ 1.64462376]\n",
    "1 1.65477 [ 1.34379935]\n",
    "2 0.470691 [ 1.18335962]\n",
    "3 0.133885 [ 1.09779179]\n",
    "4 0.0380829 [ 1.05215561]\n",
    "5 0.0108324 [ 1.0278163]\n",
    "6 0.00308123 [ 1.01483536]\n",
    "7 0.000876432 [ 1.00791216]\n",
    "8 0.00024929 [ 1.00421977]\n",
    "9 7.09082e-05 [ 1.00225055]\n",
    "10 2.01716e-05 [ 1.00120032]\n",
    "11 5.73716e-06 [ 1.00064015]\n",
    "12 1.6319e-06 [ 1.00034142]\n",
    "13 4.63772e-07 [ 1.00018203]\n",
    "14 1.31825e-07 [ 1.00009704]\n",
    "15 3.74738e-08 [ 1.00005174]\n",
    "16 1.05966e-08 [ 1.00002754]\n",
    "17 2.99947e-09 [ 1.00001466]\n",
    "18 8.66635e-10 [ 1.00000787]\n",
    "19 2.40746e-10 [ 1.00000417]\n",
    "20 7.02158e-11 [ 1.00000226]\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
